package cn.ximart.passport.service.impl;

import cn.ximart.passport.ex.ServiceException;
import cn.ximart.passport.mapper.UserMapper;
import cn.ximart.passport.pojo.dto.UserAddNewDTO;
import cn.ximart.passport.pojo.entity.User;
import cn.ximart.passport.pojo.vo.UserListItemVO;
import cn.ximart.passport.service.IUserService;
import cn.ximart.passport.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始执行新增用户的方法");
        String username = userAddNewDTO.getUsername();
        int row = userMapper.countByUsername(username);
        if(row>0){
            String message = "该用户名"+username+"已经存在!";
            throw new ServiceException(ServiceCode.ERR_EXISTED,message);
        }
        User user = new User();
        BeanUtils.copyProperties(userAddNewDTO,user);
        user.setIsEnable(1);
        user.setLoginCount(0);

        return 0;
    }

    @Override
    public void deleteById(Long id) {

    }



    @Override
    public int countUsers() {
        log.debug("开始执行查询用户数的方法");
        int count = userMapper.countUsers();
        return count;
    }

    @Override
    public List<UserListItemVO> list() {
        log.debug("开始执行查询用户列表的方法");
        List<UserListItemVO> list = userMapper.list();
        return list;
    }
}
